import { Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, OneToMany } from "typeorm";
import { EntityModel } from "@midwayjs/orm";
import { Material } from "./Material.entity";
import { MaterialDonateRecord } from "./MaterialDonateRecord.entity";

@EntityModel("material_donate", { schema: "heart_neighbor" })
export class MaterialDonate {
  @PrimaryGeneratedColumn({ type: "int", name: "id", comment: "Primary Key" })
  id!: number;

  @Column("datetime", {
    name: "create_time",
    nullable: true,
    comment: "Create Time",
    default: () => "CURRENT_TIMESTAMP",
  })
  createTime!: Date | null;

  @Column("int", { name: "count", nullable: true, comment: "需求数量" })
  count!: number | null;

  @Column("int", { name: "material_id", nullable: true, comment: "物资id" })
  materialId!: number | null;

  @Column("int", { name: "price", nullable: true, comment: "物资价格" })
  price!: number | null;

  @Column("varchar", { name: "title", nullable: true, comment: "捐赠标题" })
  title!: string | null;

  // 多对一关系：多个物资捐赠需求对应一个物资
  @ManyToOne(() => Material, (material) => material.materialDonates, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "material_id" })
  material!: Material;

  // 一对多关系：一个物资捐赠需求可以有多个捐赠记录
  @OneToMany(() => MaterialDonateRecord, (record) => record.materialDonate, { createForeignKeyConstraints: false })
  donateRecords!: MaterialDonateRecord[];
}
